package com.doradance.app.home;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.cncn.youmengsharelib.ui.SharePlatformsFragment;
import com.donkingliang.labels.LabelsView;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.MyListView;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.BabyDetailEntity;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import de.hdodenhof.circleimageview.CircleImageView;

public class BabyDetailActivity extends BaseActivity {
    @Bind(R.id.iv_baby_cover)
    ImageView mIvBabyCover;
    @Bind(R.id.civ_baby_head)
    CircleImageView mCivBabyHead;
    @Bind(R.id.tv_baby_name)
    TextView mTvBabyName;
    @Bind(R.id.tv_baby_title)
    TextView mTvBabyTitle;
    @Bind(R.id.labels_baby)
    LabelsView mLabelsBaby;
    @Bind(R.id.lv_baby_detail)
    MyListView mLvBabyDetail;
    @Bind(R.id.ll_lv)
    LinearLayout mLlLv;
    @Bind(R.id.tv_baby_age)
    TextView mTvBabyAge;
    @Bind(R.id.tv_baby_detail_more)
    TextView mTvBabyDetailMore;
    @Bind(R.id.iv_detail_right)
    ImageView mIvDetailRight;
    private int baby_id;
    private ArrayList<BabyDetailEntity.CoursesBean> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baby_id = getIntent().getIntExtra("baby_id", -1);
        initToolsBar();
        initData();
    }

    private void initData() {
        MyHttpRequest request = new MyHttpRequest(BabyDetailActivity.this);
        String babyHomeUrl = String.format(UrlCenter.BABY_HOME, baby_id);
        request.get(babyHomeUrl, null, new DataRequestListener<BabyDetailEntity>(BabyDetailEntity.class) {
            @Override
            public void onSuccess(final BabyDetailEntity o) {
                super.onSuccess(o);
                if(TextUtils.isEmpty(o.getNick_name())){
                    setTitle(getString(R.string.default_name));
                    mTvBabyName.setText(getString(R.string.default_name));
                }else{
                    setTitle(o.getNick_name());
                    mTvBabyName.setText(o.getNick_name());
                }

                if(TextUtils.isEmpty(o.getCover())){
                    Glide.with(BabyDetailActivity.this).load(R.mipmap.init_logo).into(mIvBabyCover);
                }else{
                    Glide.with(BabyDetailActivity.this).load(o.getCover()).dontAnimate()
                            .placeholder(R.mipmap.init_logo).into(mIvBabyCover);
                    final String cover = o.getCover();
                    mIvBabyCover.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showBig(cover);
                        }
                    });
                }
                if(TextUtils.isEmpty(o.getAvatar())){
                    Glide.with(BabyDetailActivity.this).load(R.mipmap.init_logo).into(mCivBabyHead);
                }else{
                    Glide.with(BabyDetailActivity.this).load(o.getAvatar()).dontAnimate()
                            .placeholder(R.mipmap.init_logo).into(mCivBabyHead);
                }


                mTvBabyTitle.setText(o.getSign());
                mTvBabyAge.setText(o.getAge_label());
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < o.getTags().size(); i++) {
                    strings.add(o.getTags().get(i));
                }
                mLabelsBaby.setLabels(strings);
                if (o.getCourse_count() > 2) {
                    mLlLv.setVisibility(View.VISIBLE);
                    mTvBabyDetailMore.setVisibility(View.VISIBLE);
                    mIvDetailRight.setVisibility(View.VISIBLE);
                    mTvBabyDetailMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MoreVideoActivity.start(BabyDetailActivity.this, o.getId());
                        }
                    });
                    setAdapterData(o.getCourses());

                } else if (o.getCourse_count() == 0) {
                    mLlLv.setVisibility(View.GONE);
                } else {
                    mLlLv.setVisibility(View.VISIBLE);
                    setAdapterData(o.getCourses());
                }

                getCustomToolbar().addRightImageButton(R.mipmap.icon_share, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareBaby(o.getNick_name(),o.getSign(),o.getAvatar(),"http://h5.doradance.cn/#/babyDetail?id="+baby_id);
                    }
                });
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void showBig(String cover) {
        final Dialog dialog = new Dialog(BabyDetailActivity.this, R.style.Transparent);
        dialog.setContentView(R.layout.activity_big_pic);

        ImageView mIvBigPic = (ImageView) dialog.findViewById(R.id.iv_big_pic);
        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.y = 40;
        dialog.onWindowAttributesChanged(lp);
        Glide.with(BabyDetailActivity.this).load(cover).asBitmap().into(mIvBigPic);
        mIvBigPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        dialog.show();
    }

    private void shareBaby(String title,String content,String image,String url) {
        SharePlatformsFragment fragment = SharePlatformsFragment.newInstance(title, content, image, url);
        fragment.setShareListener(new ShareListener() {
            @Override
            public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(BabyDetailActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShareFailed(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(BabyDetailActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShareCancel(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(BabyDetailActivity.this, "取消分享", Toast.LENGTH_SHORT).show();
            }
        });
        fragment.show(getSupportFragmentManager(), "");
    }

    public void setAdapterData(final List<BabyDetailEntity.CoursesBean> courses) {
        arrayList = new ArrayList<>();
        for (int i = 0; i < courses.size(); i++) {
            arrayList.add(courses.get(i));
        }
        BabyDetailAdapter babyDetailAdapter = new BabyDetailAdapter();
        mLvBabyDetail.setAdapter(babyDetailAdapter);
    }

    private void initToolsBar() {
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_baby_detail, null);
    }

    public static void start(Activity activity, int baby_id) {
        Intent intent = new Intent(activity, BabyDetailActivity.class);
        intent.putExtra("baby_id", baby_id);
        activity.startActivity(intent);
    }

    class BabyDetailAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View view;

            if (convertView == null) {
                //因为getView()返回的对象，adapter会自动赋给ListView
                view = inflater.inflate(R.layout.mine_collect_item, null);
            } else {
                view = convertView;
                Log.i("info", "有缓存，不需要重新生成" + position);
            }
            TextView mTvRcmdTitle = (TextView) view.findViewById(R.id.tv_rcmd_title);
            TextView mTvRcmdFree = (TextView) view.findViewById(R.id.tv_rcmd_free);
            TextView mTvRcmdTime = (TextView) view.findViewById(R.id.tv_rcmd_time);
            TextView mTvRcmdNum = (TextView) view.findViewById(R.id.tv_rcmd_num);
            XLHRatingBar ratingbarId = (XLHRatingBar) view.findViewById(R.id.ratingbarId);
            ImageView mBgIvRcmd = (ImageView) view.findViewById(R.id.bg_iv_rcmd);
            mBgIvRcmd.setAlpha(0.7f);
            RelativeLayout rlAll = (RelativeLayout) view.findViewById(R.id.rl_all);


            mTvRcmdTitle.setText(arrayList.get(position).getName());
            mTvRcmdFree.setText(arrayList.get(position).getVip_name());
            mTvRcmdTime.setText("训练周期:" + arrayList.get(position).getCycle() + "小时");
            mTvRcmdNum.setText(arrayList.get(position).getLearning_count() + "人学过");
            ratingbarId.setCountSelected(arrayList.get(position).getDifficulty());
            rlAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CoursesDetailActivity.start(BabyDetailActivity.this, arrayList.get(position).getId());
                }
            });
            Glide.with(BabyDetailActivity.this).load(arrayList.get(position).getCover()).asBitmap().dontAnimate()
                    .placeholder(R.mipmap.init_logo).into(mBgIvRcmd);
            return view;
        }
    }
}
